var editRoleUrl = null;
var editRoleMethod = null;

$(function () {
    $('#dg_role').datagrid({
                               url: ctx + 'sys/role/search_page_easyui',
                               method: "get"
                           })
})

var addRole = function () {
    editRoleUrl = ctx + 'sys/role';
    editRoleMethod = "post";
    $('#form_role_edit').form('clear');
    $('#form_role_edit select[name="type"]').val(1);
    $('#form_role_edit select[name="state"]').val(1);
    $('#dl_role_edit').dialog('open');
}

var updateRole = function () {
    editRoleUrl = ctx + 'sys/role';
    editRoleMethod = "put";
    var selected = $('#dg_role').datagrid('getSelected');
    if (!selected) {
        $.messager.alert('系统提示', "请选择您要修改的角色");
        return;
    }
    $('#form_role_edit').form('load',selected);
    $('#dl_role_edit').dialog('open');
}

var closeRoleEdit = function () {
    $('#dl_role_edit').dialog('close');
}

var submitRole = function () {
    if (!$('#form_role_edit').form("validate")) {
        return;
    }
    $.ajax({
               url: editRoleUrl,
               type: editRoleMethod,
               contentType: 'application/json',
               dataType: 'json',
               data: JSON.stringify($('#form_role_edit').serializeJSON()),
               success: function (data) {
                   closeRoleEdit();
                   //刷新表格
                   $('#dg_role').datagrid("reload");
               }
           });
}

var removeRole = function () {
    var selected = $('#dg_role').datagrid('getSelected');
    if (!selected) {
        $.messager.alert('系统提示', "请选择您要修改的角色");
        return;
    }
    $.messager.confirm("警告", "您确定要删除角色“" + selected.name + "”吗？", function (r) {
        if (!r) {
            return;
        }
        $.ajax({
                   url:ctx+'sys/role/'+selected.id,
                   type:'delete',
                   success:function (data) {
                        $('#dg_role').datagrid('reload');
                    }
               });
    })
}

var searchRole = function () {
    $('#dg_role').datagrid('load', {
        'id': $('#searRoleParams input[name="id"]').val(),
        'name': $('#searRoleParams input[name="name"]').val(),
        'type': $('#searRoleParams select[name="type"]').val()

    });
}


/*
jquery ajax 参数放入http body的注意事项：
1.contentType: 'application/json',告诉服务器接收json
2.dataType：'json'，告诉服务器请求的参数是一个json
3.JSON.stringify(json参数)，将json转为字符串。因为直接将json放入body会出现转换错误
*例如：
*               contentType: 'application/json',
               dataType: 'json',
               data: JSON.stringify($('#form_role_edit').serializeJSON()),
* */